package com.topisv.tms.service.work.outScope;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.ProcessLog;
import com.topisv.tms.entity.work.inscope.InScope;
import com.topisv.tms.entity.work.outScope.OutScope;
import com.topisv.tms.entity.work.outScope.ScopeCustomerConfig;
import com.topisv.tms.repository.ProcessLogDao;
import com.topisv.tms.repository.UserDao;
import com.topisv.tms.repository.work.outScope.OutScopeDao;
import com.topisv.tms.repository.work.outScope.ScopeCustomerConfigDao;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;

/**
 * 超区件配置_service
 * @author ch
 */
@Component
@Transactional(readOnly = true)
public class OutScopeService {

	@Autowired
	private OutScopeDao outScopeDao;
	@Autowired
	private ScopeCustomerConfigDao configDao;
	@Autowired
	private ProcessLogDao procssLogDao;
	@Autowired
	private UserDao userDao ; 
	
	public List<OutScope> loadByCompanyCode(String companyCode,String warehouseCode) {
		return this.outScopeDao.loadByCompanyCode(companyCode,warehouseCode);
	}
	public List<OutScope> loadOutScope(Map<String, String> param) {
		Specification<OutScope> spec = getWhereClause(param);
		List<OutScope> OutScopeList= this.outScopeDao.findAll(spec);
		for(OutScope outScope:OutScopeList){
			outScope.setUser(userDao.findByJobuuid(outScope.getCreaterUuid()));
		}
		return OutScopeList;
	}
	
	public OutScope loadOutScopeOne(Long id) {
		OutScope OutScope= this.outScopeDao.findOne(id);
		return OutScope;
	}
	public void saveOutScopeConfigCustomers(String outScopeId,String customerCodes) {
		OutScope outScope = this.outScopeDao.findOne(Long.valueOf(outScopeId));
		String[] customerCodeArray = customerCodes.split(",");
		//删除超区不适用商户配置
		this.configDao.deleteByOutScopeId(Long.valueOf(outScopeId));
		List<ScopeCustomerConfig> configList = new ArrayList<ScopeCustomerConfig>();
		
		//添加配置不适用商户
		for(String customerCode:customerCodeArray){
			if(StringUtils.isEmpty(outScopeId)){
				continue;
			}
			ScopeCustomerConfig config = new ScopeCustomerConfig();
			config.setOutScopeId(Long.valueOf(outScopeId));
			config.setInScopeId(null);
			config.setCustomerCode(customerCode);
			config.setCreateTime(new Date());
			configList.add(config);
		}
		//保存不适用商户配置
		this.configDao.save(configList);
		
		
		ProcessLog pLog = new ProcessLog("OUT_SCOPE_CONFIG","快递:"+outScope.getCompanyCode()+"修改了超区不适用商户 ("+customerCodes+")");
		procssLogDao.save(pLog);
	}
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<OutScope> getWhereClause(final Map<String, String> param) {
		
		return new Specification<OutScope>(){
			
		    @Override
		    public Predicate toPredicate(Root<OutScope> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      String companyCode=(String) param.get("companyCode");
		      if(StringUtils.isNotEmpty(companyCode)){
					pre.getExpressions().add(cb.equal(r.get("companyCode"),companyCode));
			  }

		      String warehouseCode=(String) param.get("warehouseCode");
		      if(StringUtils.isNotEmpty(warehouseCode)){
		    	  pre.getExpressions().add(cb.equal(r.get("warehouseCode"),warehouseCode));
		      }
		      return pre;
		    }
		  };
       }
	/**
	 * 添加超区
	 * @param companyCode
	 * @param zoneName
	 * @param zoneFullName
	 * @param jobuuid
	 * @return
	 */
	@Transactional(readOnly = false)
	public String addOutScope(String companyCode, String zoneName,String zoneFullName,String zoneType,String warehouseCode,String jobuuid) {
		try {
			
			OutScope outScope=this.outScopeDao.loadByZoneFullName(companyCode,zoneFullName,warehouseCode);
			if(outScope!=null){
				return "error_exist"; //已经存在此超区
			}
			
			outScope=new OutScope();
			outScope.setCompanyCode(companyCode);
			outScope.setCreaterUuid(jobuuid);
			outScope.setCreateTime(new Date());
			outScope.setZoneFullName(zoneFullName);
			outScope.setZoneName(zoneName);
			outScope.setZoneType(zoneType);
			outScope.setWarehouseCode(warehouseCode);
			this.outScopeDao.save(outScope);
			
			ProcessLog pLog = new ProcessLog("OUT_SCOPE","仓库:"+warehouseCode+"快递:"+companyCode+"添加了超区 ("+zoneFullName+")");
			this.procssLogDao.save(pLog);
			return "success";
			
		} catch (Exception e) {
			
			return "error";
		}
	}

	/**
	 * 删除
	 * 
	 * @param outScopeId
	 * @return
	 */
	@Transactional(readOnly = false)
	public void deleteOutScope(Long outScopeId) {
		OutScope outScope = this.outScopeDao.findOne(outScopeId);
		this.outScopeDao.delete(outScopeId);
		ProcessLog pLog = new ProcessLog("OUT_SCOPE", "仓库:" + outScope.getWarehouseCode() + "快递:"+outScope.getCompanyCode()+"删除了超区 ("+outScope.getZoneFullName() + ")");
		this.procssLogDao.save(pLog);
	}


}